package com.gzy.mylog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.gzy.mylog.model.Result;
import com.gzy.mylog.net.AuthApi;
import com.gzy.mylog.net.core.XXModelCallback;

import java.io.IOException;

import okhttp3.Call;

public class SignupActivity extends AppCompatActivity {
    EditText edt_pwd;
    EditText edt_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        findViewById(R.id.btn_commit).setOnClickListener((v) -> {
            commit();
        });

        edt_pwd = findViewById(R.id.edt_pwd);
        edt_username = findViewById(R.id.edt_username);
    }

    private void commit() {
        String edtPwdText = edt_pwd.getText().toString().trim();
        String edtUsernameText = edt_username.getText().toString().trim();

        // 数据的校验 || edtPwdText.length() > 3
        if (TextUtils.isEmpty(edtUsernameText)) {
            Toast.makeText(this, "请输入合法的用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(edtPwdText) || edtPwdText.length() < 6) {
            Toast.makeText(this, "请输入合适的密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // 访问接口
        AuthApi.signup(edtUsernameText, edtPwdText, new XXModelCallback<Result<String>>(new TypeReference<Result<String>>() {
        }) {
            @Override
            public void onResponseData(Call call, Result<String> model) {
                Toast.makeText(SignupActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                finish();
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(SignupActivity.this, "注册失败:" + message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
